﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DataAccess;

namespace iEN.Alarm
{
    public partial class SetAlarmGrp : System.Web.UI.Page
    {
        string strAccount = "";
        string strIP = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            strAccount = Session["LoginAccount"].ToString();
            strIP = Session["LoginIP"].ToString();

            if (Session["SysAdmin"].ToString() != "2")
            {
                UpdatePanel4.Visible = false;
                TabContainer1.Visible = false;
                lblMsg.Visible = true;
            }
            if (!Page.IsPostBack)
            {
                
                initialAlarmGrpMember();
                
            }
        }

        protected void initialAlarmGrpMember()
        {
            DataTable dtGrp = new DBAccess().getAlarmGrpTable();
            ddlAlarmGrp.DataSource = dtGrp;
            ddlAlarmGrp.DataTextField = "_name";
            ddlAlarmGrp.DataValueField = "_id";
            ddlAlarmGrp.DataBind();

            DataTable dtMember = new DBAccess().getAlarmMemberTable();
            ddlAlarmMember.DataSource = dtMember;
            ddlAlarmMember.DataTextField = "_name";
            ddlAlarmMember.DataValueField = "_id";
            ddlAlarmMember.DataBind();


        }
        protected void imgTip_Click(object sender, ImageClickEventArgs e)
        {
            this.Panel2.Visible = !this.Panel2.Visible;
            if (Panel2.Visible == true)
                imgTip.ImageUrl = "~/images/info_hide.gif";
            else
                imgTip.ImageUrl = "~/images/info_show.gif";
        }


        #region protected void btnAlarmGrpAdd_Click(object sender, EventArgs e) //新增告警群組
        protected void btnAlarmGrpAdd_Click(object sender, EventArgs e)
        {
            DataTable dt = new DBAccess().getAlarmGrpTable();
            DataTable dtGrpName;
            dt.DefaultView.RowFilter = " _name = '" + tbxAlarmGrpName.Text + "'";
            dtGrpName = dt.DefaultView.ToTable();
            if (dtGrpName != null)
            {
                if (dtGrpName.Rows.Count > 0)
                {
                    new DBAccess().addEventLog(strAccount, strIP, 71, "群組名稱：" + tbxAlarmGrpName.Text, "0");
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('告警群組名稱已使用, 請重新輸入告警群組名稱')", true);
                    return;
                }
            }

            bool addSuc = new DBAccess().addAlarmGrp(tbxAlarmGrpName.Text);

            if (addSuc == true)
            {
                new DBAccess().addEventLog(strAccount, strIP, 71, "群組名稱：" + tbxAlarmGrpName.Text, "1");
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('新增成功')", true);
            }
            else
            {
                new DBAccess().addEventLog(strAccount, strIP, 71, "群組名稱：" + tbxAlarmGrpName.Text, "0");
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('新增失敗')", true);
            }

            gvAlarmGrp.DataBind();
            initialAlarmGrpMember();

        }
        #endregion

        #region protected void gvAlarmGrp_RowCommand(object sender, GridViewCommandEventArgs e)
        protected void gvAlarmGrp_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int iIndex = Convert.ToInt32(e.CommandArgument);
            string GrpID = gvAlarmGrp.DataKeys[iIndex][0].ToString();
            string GrpName = gvAlarmGrp.Rows[iIndex].Cells[0].Text;

            //取得第幾列被選取 
            Boolean flag;

            if (e.CommandName == "Del")
            {
                flag = new DBAccess().deleteAlarmGrp(GrpID);

                if (flag == true)
                {
                    new DBAccess().addEventLog(strAccount, strIP, 72, "群組名稱：" + GrpName, "1");
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('告警群組刪除成功')", true);
                }
                else
                {
                    new DBAccess().addEventLog(strAccount, strIP, 72, "群組名稱：" + GrpName, "0");
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('告警群組刪除失敗')", true);
                }
                gvAlarmGrp.DataBind();
                initialAlarmGrpMember();
                ddlAlarmGrp_SelectedIndexChanged(null, null);
            }
        }
        #endregion

        #region protected void gvAlarmGrp_RowUpdating(object sender, GridViewUpdateEventArgs e)
        protected void gvAlarmGrp_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            DataTable dt = new DBAccess().getAlarmGrpTable();
            DataTable dtGrpName;
            if (e.NewValues[0].ToString() != e.OldValues[0].ToString())
            {
                dt.DefaultView.RowFilter = " _name = '" + e.NewValues[0].ToString() + "'";
                dtGrpName = dt.DefaultView.ToTable();
                if (dtGrpName != null)
                {
                    if (dtGrpName.Rows.Count > 0)
                    {
                        new DBAccess().addEventLog(strAccount, strIP, 73, "群組名稱：" + e.NewValues[0].ToString(), "0");
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('告警群組名稱已使用, 請重新輸入告警群組名稱')", true);
                        e.Cancel = true;
                    }
                }
            }

            int iIndex = e.RowIndex;
            CheckBox cbxEnabled = default(CheckBox);

            cbxEnabled = (CheckBox)gvAlarmGrp.Rows[iIndex].Cells[1].FindControl("CheckBox1");
            if (cbxEnabled.Checked)
                Session["_enabled"] = 1;
            else
                Session["_enabled"] = 0;
        }
        #endregion

        #region protected void gvAlarmGrp_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        protected void gvAlarmGrp_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            new DBAccess().addEventLog(strAccount, strIP, 73, "群組名稱：" + e.NewValues[0].ToString(), "1");
        }
        #endregion

        #region protected void btnAlarmMemberAdd_Click(object sender, EventArgs e) //新增告警人員
        protected void btnAlarmMemberAdd_Click(object sender, EventArgs e)
        {
            DataTable dt = new DBAccess().getAlarmMemberTable();
            DataTable dtName;
            dt.DefaultView.RowFilter = " _name = '" + tbxAlarmMemberName.Text + "'";
            dtName = dt.DefaultView.ToTable();
            if (dtName != null)
            {
                if (dtName.Rows.Count > 0)
                {
                    new DBAccess().addEventLog(strAccount, strIP, 74, "告警人員名稱：" + tbxAlarmMemberName.Text, "0");
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('告警人員名稱已使用, 請重新輸入告警人員名稱')", true);
                    return;
                }
            }

            bool addSuc = new DBAccess().addAlarmMember(tbxAlarmMemberName.Text, tbxAlarmMemberPhone.Text, tbxAlarmMemberEmail.Text, tbxAlarmMemberDesc.Text);

            if (addSuc == true)
            {
                new DBAccess().addEventLog(strAccount, strIP, 74, "告警人員名稱：" + tbxAlarmMemberName.Text, "1");
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('新增成功')", true);
            }
            else
            {
                new DBAccess().addEventLog(strAccount, strIP, 74, "告警人員名稱：" + tbxAlarmMemberName.Text, "0");
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('新增失敗')", true);
            }

            gvAlarmMember.DataBind();
            initialAlarmGrpMember();


        }
        #endregion

        #region protected void gvAlarmMember_RowCommand(object sender, GridViewCommandEventArgs e)
        protected void gvAlarmMember_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int iIndex = Convert.ToInt32(e.CommandArgument);
            string MemberID = gvAlarmMember.DataKeys[iIndex][0].ToString();
            string MemberName = gvAlarmMember.Rows[iIndex].Cells[0].Text;

            //取得第幾列被選取 
            Boolean flag;

            if (e.CommandName == "Del")
            {
                flag = new DBAccess().deleteAlarmMember(MemberID);

                if (flag == true)
                {
                    new DBAccess().addEventLog(strAccount, strIP, 75, "告警人員名稱：" + MemberName, "1");
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('告警人員刪除成功')", true);
                }
                else
                {
                    new DBAccess().addEventLog(strAccount, strIP, 75, "告警人員名稱：" + MemberName, "0");
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('告警人員刪除失敗')", true);
                }
                gvAlarmMember.DataBind();
                initialAlarmGrpMember();
                ddlAlarmGrp_SelectedIndexChanged(null, null);
            }
        }
        #endregion

        #region protected void gvAlarmMember_RowUpdating(object sender, GridViewUpdateEventArgs e)
        protected void gvAlarmMember_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            DataTable dt = new DBAccess().getAlarmMemberTable();
            DataTable dtName;
            if (e.NewValues[0].ToString() != e.OldValues[0].ToString())
            {
                dt.DefaultView.RowFilter = " _name = '" + e.NewValues[0].ToString() + "'";
                dtName = dt.DefaultView.ToTable();
                if (dtName != null)
                {
                    if (dtName.Rows.Count > 0)
                    {
                        new DBAccess().addEventLog(strAccount, strIP, 76, "告警人員名稱：" + e.NewValues[0].ToString(), "0");
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('告警人員名稱已使用, 請重新輸入告警人員名稱')", true);
                        e.Cancel = true;
                    }
                }
            }
        }
        #endregion

        #region protected void gvAlarmMember_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        protected void gvAlarmMember_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            new DBAccess().addEventLog(strAccount, strIP, 76, "告警人員名稱：" + e.NewValues[0].ToString(), "1");
        }
        #endregion

        #region protected void ddlAlarmGrp_DataBound(object sender, EventArgs e) //告警群組下拉選單加入第一行
        protected void ddlAlarmGrp_DataBound(object sender, EventArgs e)
        {
            ddlAlarmGrp.Items.Insert(0, new ListItem("----", ""));
        }
        #endregion

        #region protected void ddlAlarmGrp_SelectedIndexChanged(object sender, EventArgs e) //告警群組選項改變
        protected void ddlAlarmGrp_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = new DBAccess().getAlarmGrpMemberTable(ddlAlarmGrp.SelectedValue);
            gvAlarmGrpMember.DataSource = dt;
            gvAlarmGrpMember.DataBind();
            Session["AlarmGrpMember"] = dt;
        }
        #endregion

        #region protected void ddlAlarmMember_DataBound(object sender, EventArgs e) //告警群組成員下拉選單加入第一行
        protected void ddlAlarmMember_DataBound(object sender, EventArgs e)
        {
            ddlAlarmMember.Items.Insert(0, new ListItem("----", ""));
        }
        #endregion

        #region protected void ddlAlarmMember_SelectedIndexChanged(object sender, EventArgs e) //告警群組成員選項改變
        protected void ddlAlarmMember_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (ddlAlarmMember.SelectedIndex == 0)
            {
                lblMobileTel.Text = "";
                lblEmail.Text = "";
                lblDesc.Text = "";
                return;
            }

            DataTable dt = new DBAccess().getAlarmMemberTable();
            dt.DefaultView.RowFilter = "_id = '" + ddlAlarmMember.SelectedValue.ToString() + "'";
            dt = dt.DefaultView.ToTable();
            lblMobileTel.Text = dt.Rows[0]["_phone"].ToString();
            lblEmail.Text = dt.Rows[0]["_email"].ToString();
            lblDesc.Text = dt.Rows[0]["_desc"].ToString();

        }
        #endregion

        #region protected void gvAlarmGrpMember_RowCommand(object sender, GridViewCommandEventArgs e)
        protected void gvAlarmGrpMember_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            DataTable dtAlarmGrpMember = (DataTable)Session["AlarmGrpMember"];
            int iIndex = Convert.ToInt32(e.CommandArgument);
            string GrpID = dtAlarmGrpMember.Rows[iIndex][0].ToString();
            string MemberID = dtAlarmGrpMember.Rows[iIndex][1].ToString();
            string GrpName = dtAlarmGrpMember.Rows[iIndex][2].ToString();
            string MemberName = dtAlarmGrpMember.Rows[iIndex][3].ToString();

            //取得第幾列被選取 
            Boolean flag;

            if (e.CommandName == "Del")
            {
                flag = new DBAccess().deleteAlarmGrpMember(GrpID, MemberID);

                if (flag == true)
                {
                    new DBAccess().addEventLog(strAccount, strIP, 78, "告警群組名稱：" + GrpName + "/告警人員名稱：" + MemberName, "1");
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('告警人員刪除成功')", true);
                }
                else
                {
                    new DBAccess().addEventLog(strAccount, strIP, 78, "告警群組名稱：" + GrpName + "/告警人員名稱：" + MemberName, "0");
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('告警人員刪除失敗')", true);
                }
                ddlAlarmGrp_SelectedIndexChanged(null, null);
            }
        }
        #endregion

        #region protected void btnSet_Click(object sender, EventArgs e) //新增告警群組成員
        protected void btnSet_Click(object sender, EventArgs e)
        {
            DataTable dt = new DBAccess().getAlarmGrpMemberTable();
            DataTable dtGrpMember;
            dt.DefaultView.RowFilter = " _group_id = '" + ddlAlarmGrp.Text + "' AND _user_id ='" + ddlAlarmMember.Text + "'";
            dtGrpMember = dt.DefaultView.ToTable();
            if (dtGrpMember != null)
            {
                if (dtGrpMember.Rows.Count > 0)
                {
                    new DBAccess().addEventLog(strAccount, strIP, 77, "告警群組名稱：" + ddlAlarmGrp.SelectedItem.Text + "/告警人員名稱：" + ddlAlarmMember.SelectedItem.Text, "0");
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('告警群組成員已設定, 請重新設定告警群組成員')", true);
                    return;
                }
            }

            bool addSuc = new DBAccess().addAlarmGrpMember(ddlAlarmGrp.SelectedValue, ddlAlarmMember.SelectedValue);

            if (addSuc == true)
            {
                new DBAccess().addEventLog(strAccount, strIP, 77, "告警群組名稱：" + ddlAlarmGrp.SelectedItem.Text + "/告警人員名稱：" + ddlAlarmMember.SelectedItem.Text, "1");
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('新增成功')", true);
            }
            else
            {
                new DBAccess().addEventLog(strAccount, strIP, 77, "告警群組名稱：" + ddlAlarmGrp.SelectedItem.Text + "/告警人員名稱：" + ddlAlarmMember.SelectedItem.Text, "0");
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('新增失敗')", true);
            }

            ddlAlarmGrp_SelectedIndexChanged(null, null);


        }
        #endregion

    }
}